package com.hss.classloader;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.hss.classloader.demo1.ISay;
import com.hss.classloader.demo1.SayException;

import java.io.File;

import dalvik.system.DexClassLoader;
/*
jar cvf D:\xynet-auth.jar D:\com

dx --dex --output=say_something_hotfix.jar say_something.jar


*
* */
public class MainActivity extends AppCompatActivity {
    final  String TAG = "MainActivity";
    ISay mSay;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        File file = new File(Environment.getExternalStorageDirectory(),"say_something_hotfix.jar");
        if (file !=null && file.exists()){

            DexClassLoader dexClassLoader = new DexClassLoader(file.getAbsolutePath(),getApplicationContext().getExternalCacheDir().getAbsolutePath(), null, getClassLoader());
            try {
                Class   aClass =dexClassLoader.loadClass("com.hss.classloader.demo1.SayHotFix");
                mSay = (ISay) aClass.newInstance();
            } catch (ClassNotFoundException e) {
                Log.e(TAG, e.getMessage());
            } catch (IllegalAccessException e) {

                Log.e(TAG, e.getMessage());

            } catch (InstantiationException e) {
                Log.e(TAG, e.getMessage());
            }

        }

        if (mSay ==null){
            mSay = new SayException();

        }




        findViewById(R.id.tv_name).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getApplicationContext(), mSay.saySomething(),Toast.LENGTH_SHORT).show();
            }
        });
    }
}
